package org.fuxia.im.db.dao;

import java.util.List;

import org.fuxia.im.db.dao.base.impl.GenericDAOImpl;
import org.fuxia.im.entities.ChatGroup;
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public class ChatGroupDaoImpl extends GenericDAOImpl<ChatGroup, String>
{

	public List<ChatGroup> findChatGroups(String userCode)
	{
		String getGroupSql = "select cg from ChatGroup cg,UserChatGroup ucg where ucg.userCode=:userCode and cg.id=ucg.chatGroupId";
		List<ChatGroup> result = findByNamedParam(getGroupSql, new String[] { "userCode" }, new Object[] { userCode });
		if(result != null) {
			for(ChatGroup cg : result) {
				cg.setUsers(findChatGroupUsers(cg.getId()));
			}
		}
		return result;
	}
	
	public ChatGroup findByGroupId(String groupId) {
		ChatGroup cg = getByPK(groupId);
		if(cg!=null) {
			String hql = "select ucg.userCode,ucg.nickName from UserChatGroup ucg where ucg.chatGroupId=:cgId" ;
			List<Object[]> ucg = findByNamedParam(hql, new String[]{"cgId"}, new String[]{cg.getId()}) ;
			cg.setUsers(ucg);
		}
		return cg ;
	}
	
	public List<Object[]> findChatGroupUsers(String groupID) {
		String hql = "select ucg.userCode,ucg.nickName from UserChatGroup ucg where ucg.chatGroupId=:cgId" ;
		return findByNamedParam(hql, new String[]{"cgId"}, new String[]{groupID}) ;
	}
	

}
